<?php
/*qq登录*/

namespace App\Lib;

class qq
{
    private $conf;
    private $appId;
    private $appKey;
    protected $url = 'https://graph.qq.com';
    
    public function __construct()
    {
        $this->conf = config('thirdpart_login');
        $this->appId = $this->conf['qq']['appId'];
        $this->appKey = $this->conf['qq']['appKey'];
    }
    
    /**
     * @desc 运行
     * @param $accessToken
     * @return mixed
     * */
    public function run($accessToken)
    {
        $ret = self::getUserInfo($accessToken);
        if ($ret === false) {
            return false;
        }
        return $ret;
    }
    
    /**
     * @desc 获得用户详细信息
     * @param @accessToken
     * @return mixed
     * */
    private function getUserInfo($accessToken)
    {
        $api = $this->url . "/user/get_user_info";
        $openId = self::getUserSample($accessToken);
        if ($openId === false) {
            return false;
        }
        $param = [
            'access_token' => $accessToken,
            'oauth_consumer_key' => $this->appId,
            'openid' => $openId,
        ];
        
        $result = curl_request($api, $param, 'urlencoded', 'GET');
        $ret = json_decode($result, true);
        if ($ret['ret'] != 0) {
            return false;
        }
        if ($ret['gender'] == '男') {
            $gender = 1;
        } elseif ($ret['gender'] == '女') {
            $gender = 2;
        } else {
            $gender = 3;
        }
        return [
            'unionid' => '',
            'openid' => $openId,
            'nickname' => $ret['nickname'],
            'gender' => $gender,
            'province' => $ret['province'],
            'city' => $ret['city'],
            'portrait' => $ret['figureurl_qq_2']
        ];
    }
    
    /**
     * @desc 获得用户信息 openid
     * @param @accessToken
     * @return mixed
     * */
    private function getUserSample($accessToken)
    {
        $api = $this->url . "/oauth2.0/me";
        $param = [
            'access_token' => $accessToken,
        ];
        $result = curl_request($api, $param, 'urlencoded', 'GET');
        $str = '{"error": 1}';
        if (strpos($result, "callback") !== false) {
            $lpos = strpos($result, "(");
            $rpos = strrpos($result, ")");
            $str = substr($result, $lpos + 1, $rpos - $lpos - 1);
        }
        $ret = json_decode($str, true);
        if (isset($ret['openid'])) {
            return $ret['openid'];
        }
        return false;
    }
    
    //accessToken续期
    private function refreshToken($refreshToken)
    {
        $api = $this->url . "/oauth2.0/token";
        $param = [
            'client_id' => $this->appId,
            'grant_type' => 'refresh_token',
            'client_secret' => $this->appKey,
            'refresh_token' => $refreshToken,
        ];
        $ret = curl_request($api, $param, 'urlencoded', 'GET');
        return $ret;
    }
    
}